<!DOCTYPE html>
<html lang="en" class="app"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://rbac.rbacshiro.com"
>
<head th:include="template/top"></head>
<body>
<section class="vbox">
    <header th:include="template/header" class="bg-dark dk header navbar navbar-fixed-top-xs"></header>
    <section>
        <section class="hbox stretch">
            <!-- .aside -->
            <aside th:include="template/menu" class="bg-dark lter aside-md hidden-print" id="nav"></aside>
            <!-- /.aside -->
            <section id="content">
                <section class="hbox stretch">
                    <aside>
                        <section class="vbox">
                            <section class="scrollable padder">
                                <ul class="breadcrumb no-border no-radius b-b b-light pull-in">
                                    <li><a href="index.html"><i class="fa fa-home"></i> 首页</a></li>
                                    <li class="active">系统控制</li>
                                    <li class="active">菜单管理</li>
                                </ul>
                                <header class="header bg-white b-b clearfix">
                                    <div class="row m-t-sm">
                                        <div class="col-sm-8 m-b-xs">
                                            <shiro:hasPermission values="RBAC_SYSTEM_MENU_ADD" logical="AND">
                                            <a href="javascript:" data-target="menu-add.html" id="addMenu" class="btn btn-sm btn-default"><i class="fa fa-plus"></i> 添加</a>
                                            </shiro:hasPermission>
                                        </div>
                                    </div>
                                </header>
                                <section class="scrollable wrapper w-f">
                                    <section class="panel panel-default">
                                        <div class="table-responsive">
                                            <table class="table table-striped m-b-none">
                                                <thead>
                                                <tr>
                                                    <th>菜单名称</th>
                                                    <th>链接地址</th>
                                                    <th>排序</th>
                                                    <th></th>
                                                </tr>
                                                </thead>
                                                <tbody id="data-list">
                                                    <tr>
                                                        <td>
                                                            {{if pid > 0}}
                                                            &nbsp;&nbsp;&nbsp;&nbsp;|
                                                            {{/if}}
                                                            <% for(var j = 0; j < deepNum; j ++) {%>
                                                            —
                                                            <%}%>
                                                            <i class="fa {{item.ico}} icon"> </i>&nbsp;
                                                            {{item.name}}
                                                        </td>
                                                        <td>{{item.mca}}</td>
                                                        <td>{{item.orderNumber}}</td>
                                                        <td>
                                                            <div class="doc-buttons">
                                                                <shiro:hasPermission values="RBAC_SYSTEM_MENU_ADD" logical="AND">
                                                                <button onclick="addMenu(this)" data-target="menu-add.html" data-uuid="{{item.uuid}}" class="btn btn-sm btn-primary" title="添加子权限"><i class="fa fa-plus text"></i></button>&nbsp;
                                                                </shiro:hasPermission>
                                                                <shiro:hasPermission values="RBAC_SYSTEM_MENU_EDIT" logical="AND">
                                                                <button onclick="editMenu(this)" data-target="menu-edit.html" data-uuid="{{item.uuid}}" class="btn btn-sm btn-warning" title="修改"><i class="fa fa-wrench text"></i></button>&nbsp;
                                                                </shiro:hasPermission>
                                                                <shiro:hasPermission values="RBAC_SYSTEM_MENU_DELETE" logical="AND">
                                                                <button onclick="deleteMenu(this)" data-target="menu/delete?uuid={{item.uuid}}" class="btn btn-sm btn-danger" title="删除"><i class="fa fa-trash-o text"></i></button>&nbsp;
                                                                </shiro:hasPermission>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </section>
                                </section>
                            </section>
                            <footer class="footer bg-white b-t">
                                <div class="row text-center-xs">
                                    <div class="col-md-6 hidden-sm"></div>
                                    <div class="col-md-6 col-sm-12 text-right text-center-xs" id="pagination">
                                        <!--   分页区 -->
                                    </div>
                                </div>
                            </footer>
                        </section>
                    </aside>
                </section>
                <a href="#" class="hide nav-off-screen-block" data-toggle="class:nav-off-screen" data-target="#nav"></a> </section>
            <aside class="bg-light lter b-l aside-md hide" id="notes">
                <div class="wrapper">Notification</div>
            </aside>
        </section>
    </section>
</section>
<!--  当前操作UUID -->
<input type="hidden" name="uuid" value="" id="uuid">
<script th:replace="template/js"></script>
<script src="menu.js"></script>
<script>
    var menuTemplate = loadArtTemplate('data-list');
</script>
<shiro:hasPermission values="RBAC_SYSTEM_MENU" logical="AND">
    <script src="jquery.paging.min.js"></script>
    <script>
        var showPage = function(pid, resultMap, deepNum){
            var trList = [];
            var list = resultMap[pid];
            for(var k in list) {
                var item = list[k];
                var render = template.compile(menuTemplate);

                trList.push(render({
                    item:item,
                    pid:pid,
                    deepNum:deepNum
                }));
                if(resultMap[item.id] !== undefined) {
                    trList.push(showPage(item.id, resultMap, deepNum + 1));
                }
            }
            return trList.join('');
        };

        commonAjax({
            url : "menu/list",
            data:{},
            dataType:'json',
            success:function(result) {
                var resultMap = {};
                for(var k in result.data) {
                    var item = result.data[k];
                    if(resultMap[item.pid] == undefined) {
                        resultMap[item.pid] = [];
                    }
                    resultMap[item.pid].push(item);
                }
                var trList = showPage(0, resultMap, 0);
                $('#data-list').html(trList);
            }
        });
    </script>

    <!--添加菜单-->
    <shiro:hasPermission values="RBAC_SYSTEM_MENU_ADD" logical="AND">
    <script>
        $('#addMenu').click(function(){
            $('input[name="uuid"]').val('');
            layer.open({
                type:2,
                content:[$(this).data('target'), 'no'],
                area:['550px', '500px']
            });
        });
        function addMenu(_self) {
            layer.open({
                type:2,
                content:[$(_self).data('target'), 'no'],
                area:['550px', '500px']
            });
            $('input[name="uuid"]').val($(this).data('uuid'));
        }
    </script>
    </shiro:hasPermission>
    <!--编辑菜单-->
    <shiro:hasPermission values="RBAC_SYSTEM_MENU_EDIT" logical="AND">
    <script>
        function editMenu(_self) {
            $('input[name="uuid"]').val($(_self).data('uuid'));
            layer.open({
                type:2,
                content:[$(_self).data('target'), 'no'],
                area:['550px', '500px']
            });
        }
    </script>
    </shiro:hasPermission>
    <!--删除菜单-->
    <shiro:hasPermission values="RBAC_SYSTEM_MENU_DELETE" logical="AND">
    <script>
        function deleteMenu(_self) {
            layer.open({
                type : 1,
                content:'<div style="padding: 20px 80px;">你确定要删除吗？</div>',
                btn:['确&nbsp;定', "再考虑一下"],
                yes : function(){
                    commonAjax({
                        url : $(_self).data('target'),
                        success : function(result) {
                            if(result.success) {
                                window.location.reload();
                            } else {
                                layer.msg(result.message);
                            }
                        }
                    });
                }
            });
        }
    </script>
    </shiro:hasPermission>
</shiro:hasPermission>
</body>
</html>